package com.metacube.metaconnect.model;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LoginForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String email;
	private String password;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {

		ActionErrors errorList = new ActionErrors();

		try {
			if (password.equals(""))
				errorList.add("password", new ActionMessage(
						"MetaConnect.error.password"));
			if (email.trim().equals(""))
				errorList.add("email", new ActionMessage(
						"MetaConnect.error.email"));
			else if (!email
					.matches("^[A-Za-z][_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"))
				errorList.add("email", new ActionMessage(
						"MetaConnect.error.invalidEmail"));

			if (!errorList.isEmpty())
				request.setAttribute("loginError", "yes");

		} catch (Exception e) {
			e.printStackTrace();
		}

		return errorList;
	}

}
